package com.jiecao.news.jiecaonews;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;

import com.jiecao.news.jiecaonews.activity.NewsActivity;
import com.jiecao.news.jiecaonews.service.StartService;
import com.jiecao.news.jiecaonews.util.AlarmUtil;
import com.jiecao.news.jiecaonews.util.ManagerUtil;
import com.jiecao.news.jiecaonews.util.SharedPreferencesUtil;
import com.jiecao.news.jiecaonews.util.constants.Constants;
/**
 * loading
 *更新
 * 创建桌面图标
 * @author lidongwei
 *
 */
public class MainActivity extends BaseActivity {
	private static final String TAG = "MainActivity";
	private String localVer;
	private SharedPreferences spn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		registerReceiver(finishbr, new IntentFilter(finish_br_name));
		spn = getSharedPreferences(Constants.PREFS_NAME, 0);
		localVer = ManagerUtil.getVerName(this);
		Boolean first = spn.getBoolean(Constants.FIRST_START, true);
		String version = spn.getString(Constants.STOR_VERSION, "");
		// 是第一次启动或更新后启动,添加开机方式
		if ((first||!localVer.equals(version))&&!isAddShortCut()) {
			addShortCut();
	         //jpush设置推送为声音提示（出现不能在通知栏显示问题）
//	         BasicPushNotificationBuilder builder = new BasicPushNotificationBuilder(this);
//	         builder.notificationDefaults =Notification.DEFAULT_SOUND;
//	         JPushInterface.setDefaultPushNotificationBuilder ( builder);
		}
		SharedPreferencesUtil.writeFirst(this,localVer);
		
				// 取消定时器（这里面会关闭service）
				AlarmUtil.stopAlarm(StartService.class,MainActivity.this);
				// 启动定时器
				AlarmUtil.startAlarm( StartService.class,MainActivity.this);
		
		
		//停留两秒
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					Thread.sleep(2000);
					Intent intent  = new Intent(MainActivity.this,NewsActivity.class);
					startActivity(intent);
					finish();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
		t.start();
	}
	//判断是否已经添加了桌面快捷方式
	public boolean isAddShortCut() {

        boolean isInstallShortcut = false;
        final ContentResolver cr = getContentResolver();

        int versionLevel = android.os.Build.VERSION.SDK_INT;
        String AUTHORITY = "com.android.launcher2.settings";
        
        //2.2以上的系统的文件文件名字是不一样的
        if (versionLevel >= 8) {
            AUTHORITY = "com.android.launcher2.settings";
        } else {
            AUTHORITY = "com.android.launcher.settings";
        }

        final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
                + "/favorites?notify=true");
        Cursor c = cr.query(CONTENT_URI,
                new String[] { "title", "iconResource" }, "title=?",
                new String[] { getString(R.string.app_name) }, null);

        if (c != null && c.getCount() > 0) {
            isInstallShortcut = true;
        }
        return isInstallShortcut;
    }
	//创建桌面快捷方式
public void addShortCut(){
        
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        // 设置属性
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
        ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this.getApplicationContext(), R.drawable.ic_launcher);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
 
        // 是否允许重复创建
        shortcut.putExtra("duplicate", false);
        
        //设置桌面快捷方式的图标
        Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.ic_launcher);        
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
        
        //点击快捷方式的操作
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setClass(MainActivity.this, MainActivity.class);
        
        // 设置启动程序
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
        
        //广播通知桌面去创建
        this.sendBroadcast(shortcut);
    }


}
